import httpclient from 'httpclient';
import api from '../../../../api/api_kind.js';
// let id = 1000;
export default {
    data: function () {
        return {
            visible: false,//防止重复提交
            filterText: '',
            treeData: [],
            menuForm: {},
            rules: {
                name: [
                    {required: true, message: '请输入菜单名称', trigger: 'blur'},
                    {min: 2, max: 10, message: '长度在 2 到 100 个字符', trigger: 'blur'}
                ],
                alias: [
                    {required: true, message: '请输入菜单别名', trigger: 'blur'},
                    {min: 2, max: 10, message: '长度在 2 到 100 个字符', trigger: 'blur'}
                ],
                code: [
                    {required: true, message: '请输入菜单编码', trigger: 'blur'},
                    {min: 2, max: 100, message: '长度在 2 到 100 个字符', trigger: 'blur'}
                ],
                pathname: [
                    {required: true, message: '请输入菜单路由', trigger: 'blur'},
                    {min: 2, max: 100, message: '长度在 2 到 100 个字符', trigger: 'blur'}
                ]
            },
        };
    },
    created() {
        this.getData(api.BASE.MENU.MENU_ALL_4_TREE);
    },
    watch: {
        filterText(val) {
            this.$refs.tree.filter(val);
        }
    },
    methods: {
        filterNode(value, data) {
            if (!value) {
                return true;
            }
            return data.label.indexOf(value) !== -1;
        },
        getData: function (api) {
            const that = this;
            httpclient.sendGet(api).then(
                (response) => {
                    // console.log(JSON.stringify(response.data));
                    that.treeData = response.data;
                }
            ).fail((error) => {
                httpclient.errorPage("error", error.errorMsg, true);
            });
        },
        loadNode: function (node) {
            const that = this;
            that.visible = true;
            httpclient.sendGet(api.BASE.MENU.TREE_NODE_BY_ID, {id: node.key}).then(
                (response) => {
                    // console.log(node.id);
                    that.menuForm = response.data;
                    that.menuForm.id = node.key;
                    that.menuForm.parentName = response.data.parentName;
                    that.visible = false;
                }
            ).fail((error) => {
                httpclient.errorPage("error", error.errorMsg, true);
            });
        },
        append(node) {
            //清空form，同时赋值父节点
            const that = this;
            that.menuForm = {};
            that.menuForm.id = null;
            that.menuForm.parent = node.key;
            that.menuForm.parentName = node.label;
            // console.info("ff:"+JSON.stringify(that.menuForm));
        },

        remove(node, data) {
            if (node.childNodes.length>0) {
                httpclient.errorPage("warning", "该权限下还有子权限，请先删除子权限。", true);
                return;
            }
            const that = this;
            that.$confirm('此操作将删除该权限, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                httpclient.sendDelete(api.BASE.MENU.DELETE, {id: node.key}).done((res) => {
                    if (res.code === 1) {//成功
                        const parent = node.parent;
                        const children = parent.data.children || parent.data;
                        const index = children.findIndex(d => d.id === data.id);
                        children.splice(index, 1);
                        httpclient.errorPage("success", "删除成功", true);
                    } else {
                        httpclient.errorPage("error", "删除失败", true);
                    }
                });
            }).catch(() => {
                httpclient.errorPage("warning", "取消删除", true);
            });
        },
        saveData(formName) {
            // console.info(this.$refs.tree.getCurrentNode());return;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (valid) {//如果验证通过
                        this.visible = true;
                        this.menuForm.url = this.menuForm.pathname;//数据库中，这两个字段存的是同一个值，后面才发现，路由走的是url，所以在这里赋个值。
                        httpclient.sendPost(api.BASE.MENU.SAVE_OR_UPDATE, this.menuForm).done((res) => {
                            this.visible = false;
                            // console.info("1:"+res.data);
                            // console.info("2:"+this.menuForm.id);
                            if (res.code === 1) {//成功
                                if (this.menuForm.id !== null && this.menuForm.id !== '') {//更新
                                    this.menuForm.id = res.data;
                                    this.$refs.tree.getCurrentNode().label = this.menuForm.name;
                                    this.$refs.tree.getCurrentNode().id = res.data;
                                    this.$refs.tree.getCurrentNode().parentId = this.menuForm.parent;
                                } else {//添加
                                    let child = {};
                                    this.menuForm.id = res.data;
                                    child.id = res.data;
                                    child.label = this.menuForm.name;
                                    child.children = [];
                                    // console.info(this.$refs.tree.getCurrentNode());
                                    // console.info(this.$refs.tree.getCurrentNode().children);
                                    // console.info(child);
                                    this.$refs.tree.getCurrentNode().children.push(child);
                                }
                                httpclient.errorPage("success", "保存成功", true);
                            } else {
                                //失败
                                httpclient.errorPage("error", "保存失败", true);
                            }
                        }).fail((error) => {
                            this.visible = false;
                            httpclient.errorPage("error", error.errorMsg, true);
                        });
                    }
                } else {
                    //页面校验失败
                    return false;
                }
            });
        },
        resetProductMes() {
            this.menuForm = {};
            this.$refs.menu.resetFields();
        },//重置表单
    }
};
